package interfaceGraphique;

import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Frame;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JComboBox;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import metier.ElementListe;
import metier.ExceptionMetier;
import facade.FacadePersonne;
import facade.FacadeProgramme;

public class GestionServicesGUI extends JDialog implements ActionListener {

	  //  @jve:decl-index=0:
	
	private FacadeProgramme facadeProgramme = new FacadeProgramme();  //  @jve:decl-index=0:
	private FacadePersonne facadePersonne = new FacadePersonne();  //  @jve:decl-index=0:
	private static final long serialVersionUID = 1L;
	private JPanel servicePan = null;
	private JLabel lEnseignant = null;
	private JComboBox listeEnseignant = null;
	private JLabel lGroupe = null;
	private JComboBox listeGroupe = null;
	private JComboBox listeTypeHeure = null;
	private JLabel lTypeHeure = null;
	private JPanel boutonPan = null;
	private JButton bValider = null;
	private JButton bAnnuler = null;
	
	private int idService;
	public int idSee;
	public String action;
	
	/**
	 * @param owner
	 */
	public GestionServicesGUI(Frame owner,int IDservice, String action,int IDsee) {
		super(owner);
		this.idService = IDservice;
		this.action = action;
		this.idSee = IDsee;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(350, 200);
		this.setResizable(false);
		this.setModal(true);
		this.setContentPane(getServicePan());
	}

	/**
	 * This method initializes servicePan
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getServicePan() {
		if (servicePan == null) {
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 1;
			gridBagConstraints21.anchor = GridBagConstraints.WEST;
			gridBagConstraints21.gridy = 3;
			GridBagConstraints gridBagConstraints20 = new GridBagConstraints();
			gridBagConstraints20.gridx = 0;
			gridBagConstraints20.anchor = GridBagConstraints.CENTER;
			gridBagConstraints20.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints20.gridy = 2;
			lTypeHeure = new JLabel();
			lTypeHeure.setText("Type d'heure :");
			GridBagConstraints gridBagConstraints19 = new GridBagConstraints();
			gridBagConstraints19.fill = GridBagConstraints.BOTH;
			gridBagConstraints19.gridy = 2;
			gridBagConstraints19.weightx = 1.0;
			gridBagConstraints19.insets = new Insets(15, 0, 15, 10);
			gridBagConstraints19.anchor = GridBagConstraints.WEST;
			gridBagConstraints19.gridx = 1;
			GridBagConstraints gridBagConstraints18 = new GridBagConstraints();
			gridBagConstraints18.fill = GridBagConstraints.BOTH;
			gridBagConstraints18.gridy = 1;
			gridBagConstraints18.weightx = 1.0;
			gridBagConstraints18.insets = new Insets(15, 0, 0, 10);
			gridBagConstraints18.anchor = GridBagConstraints.WEST;
			gridBagConstraints18.gridx = 1;
			GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
			gridBagConstraints17.gridx = 0;
			gridBagConstraints17.anchor = GridBagConstraints.SOUTHWEST;
			gridBagConstraints17.insets = new Insets(0, 10, 4, 10);
			gridBagConstraints17.gridy = 1;
			lGroupe = new JLabel();
			lGroupe.setText("Groupe :");
			GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
			gridBagConstraints16.fill = GridBagConstraints.BOTH;
			gridBagConstraints16.gridy = 0;
			gridBagConstraints16.weightx = 1.0;
			gridBagConstraints16.insets = new Insets(0, 0, 0, 10);
			gridBagConstraints16.anchor = GridBagConstraints.WEST;
			gridBagConstraints16.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.anchor = GridBagConstraints.WEST;
			gridBagConstraints.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints.gridy = 0;
			lEnseignant = new JLabel();
			lEnseignant.setText("Enseignant :");
			servicePan = new JPanel();
			servicePan.setBorder(BorderFactory.createTitledBorder("Gestion des services"));
			servicePan.setLayout(new GridBagLayout());
			servicePan.add(lEnseignant, gridBagConstraints);
			servicePan.add(getListeEnseignant(), gridBagConstraints16);
			servicePan.add(lGroupe, gridBagConstraints17);
			servicePan.add(getListeGroupe(), gridBagConstraints18);
			servicePan.add(getListeTypeHeure(), gridBagConstraints19);
			servicePan.add(lTypeHeure, gridBagConstraints20);
			servicePan.add(getBoutonPan(), gridBagConstraints21);
		}
		return servicePan;
	}

	/**
	 * This method initializes listeEnseignant	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getListeEnseignant() {
		if (listeEnseignant == null) {
			listeEnseignant = new JComboBox();
			listeEnseignant.setPreferredSize(new Dimension(200,20));
			 refreshListeEnseignant();
		}
		return listeEnseignant;
	}

	/**
	 * This method initializes listeGroupe	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getListeGroupe() {
		if (listeGroupe == null) {
			listeGroupe = new JComboBox();
			listeGroupe.setPreferredSize(new Dimension(200,20));
			refreshListeGroupe();
		}
		return listeGroupe;
	}

	/**
	 * This method initializes listeTypeHeure	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getListeTypeHeure() {
		if (listeTypeHeure == null) {
			listeTypeHeure = new JComboBox();
			listeTypeHeure.setPreferredSize(new Dimension (200,20));
			refreshListeTypeHeure();
		}
		return listeTypeHeure;
	}

	/**
	 * This method initializes boutonPan	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getBoutonPan() {
		if (boutonPan == null) {
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 1;
			gridBagConstraints2.gridy = 0;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.insets = new Insets(0, 0, 0, 10);
			gridBagConstraints1.anchor = GridBagConstraints.WEST;
			gridBagConstraints1.gridy = 0;
			boutonPan = new JPanel();
			boutonPan.setLayout(new GridBagLayout());
			boutonPan.add(getBValider(), gridBagConstraints1);
			boutonPan.add(getBAnnuler(), gridBagConstraints2);
		}
		return boutonPan;
	}

	/**
	 * This method initializes bValider	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBValider() {
		if (bValider == null) {
			bValider = new JButton();
			bValider.setText("Valider");
			bValider.addActionListener(this);
		}
		return bValider;
	}

	/**
	 * This method initializes bAnnuler	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBAnnuler() {
		if (bAnnuler == null) {
			bAnnuler = new JButton();
			bAnnuler.setText("Annuler");
			bAnnuler.addActionListener(this);
		}
		return bAnnuler;
	}

	private void afficherStatut(String message) {
		AdminGUI.jLabelStatus.setText(message);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		try{
		if (e.getSource().equals(bAnnuler))
		{	
			this.setVisible(false);
			dispose();
		}
		
		if (e.getSource().equals(bValider))
		{	
		
			if( action == "a" )
			{
				facadeProgramme.creerSee();
				facadeProgramme.getSee().load(this.idSee);
			
				facadePersonne.creerEnseignant(null, null, null);
				facadePersonne.getEnseignant().load(((ElementListe)getListeEnseignant().getSelectedItem()).getId());
					
				facadePersonne.creerGroupe(null,0,null);
				facadePersonne.getGroupe().load(((ElementListe)getListeGroupe().getSelectedItem()).getId());
				
				facadeProgramme.creerTypeHeure(null);
				facadeProgramme.getTypeheure().load(((ElementListe)getListeTypeHeure().getSelectedItem()).getId());
				
				facadeProgramme.creerService(facadeProgramme.getSee(),facadePersonne.getEnseignant(),facadeProgramme.getTypeheure(),facadePersonne.getGroupe());
				facadeProgramme.ajouterService();
				afficherStatut("Ajout effectu�");
				
				this.setVisible(false);
				dispose();
				
			}
			
			// Modification d'une service
			else if (action == "m")
			{
				
				facadeProgramme.creerSee();
				facadeProgramme.getSee().load(this.idSee);
			
				facadePersonne.creerEnseignant(null, null, null);
				facadePersonne.getEnseignant().load(((ElementListe)getListeEnseignant().getSelectedItem()).getId());
					
				facadePersonne.creerGroupe(null,0,null);
				facadePersonne.getGroupe().load(((ElementListe)getListeGroupe().getSelectedItem()).getId());
				
				facadeProgramme.creerTypeHeure(null);
				facadeProgramme.getTypeheure().load(((ElementListe)getListeTypeHeure().getSelectedItem()).getId());
				
				facadeProgramme.creerService(null, null, null, null);
				facadeProgramme.getService().load(idService);
				facadeProgramme.modifierService(facadePersonne.getEnseignant(),facadePersonne.getGroupe(),facadeProgramme.getTypeheure(),facadeProgramme.getSee());
				afficherStatut("Modification effectu�e");
				
				//Reorganisation des elements graphiques
				this.setVisible(false);
				dispose();
			}
			
			}
			
		
		}catch (ExceptionMetier ex) {
            afficherStatut(ex.toString());
        	ex.printStackTrace();
		}
		
	}
	
	 private void refreshListeEnseignant() {
	        try {
	            ArrayList<ElementListe> liste = facadePersonne.creerListeEnseignant();
	            getListeEnseignant().removeAllItems();
	           
	            for (int i = 0; i < liste.size(); i++) {
	            	getListeEnseignant().addItem(liste.get(i));
	            }
	        } catch (ExceptionMetier ex) {
	            afficherStatut(ex.toString());
	        	ex.printStackTrace();
	        }

	    }
	
	 private void refreshListeGroupe() {
	        try {
	            ArrayList<ElementListe> liste = facadePersonne.creerListeGroupe();
	            getListeGroupe().removeAllItems();
	           
	            for (int i = 0; i < liste.size(); i++) {
	            	getListeGroupe().addItem(liste.get(i));
	            }
	        } catch (ExceptionMetier ex) {
	            afficherStatut(ex.toString());
	        	ex.printStackTrace();
	        }

	    }
	 
	 private void refreshListeTypeHeure() {
	        try {
	            ArrayList<ElementListe> liste = facadeProgramme.creerListeTypeHeure();
	            getListeTypeHeure().removeAllItems();
	           
	            for (int i = 0; i < liste.size(); i++) {
	            	getListeTypeHeure().addItem(liste.get(i));
	            }
	        } catch (ExceptionMetier ex) {
	            afficherStatut(ex.toString());
	        	ex.printStackTrace();
	        }

	    }
	

}  //  @jve:decl-index=0:visual-constraint="104,6"
